package com.gem.controller;

import com.gem.entity.Room;
import com.gem.entity.vo.MainVO;
import com.gem.factory.room.RoomFactory;
import com.gem.netty.session.SessionFactory;
import com.gem.service.RoomService;
import com.gem.factory.user.UserFactory;
import com.gem.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 主页,用于获取房间列表
 * Author: Xhy
 * CreateTime: 2022-09-17 23:40
 */

@RestController
@CrossOrigin
public class IndexController {

    @GetMapping("/roomsInfo")
    public R main(){
        Map<String, Room> roomMap = RoomFactory.listRooms();
        AtomicInteger run = new AtomicInteger(0);
        int count = roomMap.size();
        roomMap.forEach((k,v)->{
            if (v.getState()){
                run.getAndIncrement();
            }
        });
        MainVO mainVO = new MainVO();
        mainVO.setRooms(count);
        mainVO.setRunRooms(run.get());
        mainVO.setWaitRooms(count-run.get());
        mainVO.setOnLineUser(SessionFactory.getSession().count());
        return R.ok().data("data",mainVO);
    }


}
